#include "DisplayProperties.hpp"
#include "ioctl.h"
#include "regmgr.h"
#include "propnames.h"
#include "CheckSign.h"
#include "Display_OEM.h"
#include "Display_VIA.h"
#include "Display_LX.h"
#include "Display_eBox2300.h"
#include "Display_eBox2300sx.h"

//void CheckSign( TCHAR * settings, DWORD *width, DWORD *height,DWORD *freq, DWORD *colorDepth);

void CDisplayPropPage::OEMDisplayExecute( DWORD testMode )
{
	int result = IDCANCEL;

	DWORD width,height,freq, colorDepth;
	UINT selIndex=0;
	UINT colIndex=0 ;
	HKEY hKey = NULL;
	
	TCHAR szSelDisplay[DISPLAYCOMBO_LEN]={0};
	TCHAR szSelColor[COLORCOMBO_LEN]={0};

	PCOLORINFO tempColorInfo = NULL;

	selIndex = m_displayChoices.SendMessage( CB_GETCURSEL,0,0 );
	
	if ((selIndex == CB_ERR) || (m_displayChoices.SendMessage ( CB_GETLBTEXT,
		(WPARAM)selIndex,
		(LPARAM)szSelDisplay) 
		== CB_ERR ))
	{
		RETAILMSG (1,(L"CDisplayPropPage::SetDisplayInfo( )--CB_GETLBTEXT failed"));
		return;
	}


	if ( m_bWizard )
	{
		selIndex = m_colorChoices.SendMessage( CB_GETCURSEL,0,0 );
		
		if ((selIndex == CB_ERR) || (m_colorChoices.SendMessage ( CB_GETLBTEXT,
			(WPARAM)selIndex,
			(LPARAM)szSelColor) 
			== CB_ERR ))
		{
			RETAILMSG (1,(L"CDisplayPropPage::SetDisplayInfo( )--CB_GETLBTEXT failed"));
			return;
		}


		for ( tempColorInfo = headColorInfo; tempColorInfo; tempColorInfo=tempColorInfo->next)
		{

			if ( wcscmp(tempColorInfo->colorTableName,szSelColor)==0 )
			{				
				colorDepth = tempColorInfo->colorTableValue;

				break;
			}
		}

	} 
	else 
	{
		if ( !GetCurrentColorSetting(&colorDepth) )
		{
			RETAILMSG (1,(L"CDisplayPropPage::SetDisplayInfo( )--CB_GETLBTEXT failed"));
			return;
		}
	}

	width  = 640;
	height = 480;
	freq   = 60;
	
	if ( CheckSign(TEXT("\\Windows\\macs.dat"), TEXT("\\Windows\\macs.sign")) )
	{
		_stscanf(szSelDisplay, TEXT("%d x %d @%d"), &width, &height,&freq);
	} 
	
	if ( testMode == FALSE )
	{
			oemDisplay->DisplayUpdate(width, height, colorDepth, freq);
	} else {
		ShowWindow(m_hwnd, SW_MAXIMIZE);    
		EnableWindow(m_hwnd, FALSE);
					
			oemDisplay->DisplayTest(width, height, colorDepth, freq);

		EnableWindow(m_hwnd, TRUE);    
		ShowWindow(m_hwnd, SW_RESTORE);
	}
}


void CDisplayPropPage::OEMDisplayUpdate()
{
	OEMDisplayExecute(FALSE);

}

void CDisplayPropPage::OEMDisplayTest()
{
	OEMDisplayExecute(TRUE);
}


void CDisplayPropPage::OEMDisplayInit()
{
	Display_OEM* oem;

	oem = (Display_OEM *) new Display_eBox2300sx();
	if ( oem->IsSupported() )
	{
		oemDisplay = oem;
		return;
	}
	
	if ( oem )
	{
		delete oem;
		oem = NULL;
	}

	oem = (Display_OEM *) new Display_eBox2300();
	if ( oem->IsSupported() )
	{
		oemDisplay = oem;
		return;
	}

	if ( oem )
	{
		delete oem;
		oem = NULL;
	}

	oem = (Display_OEM *) new Display_LX();
	if ( oem->IsSupported() )
	{
		oemDisplay = oem;
		return;
	} 
	
	if ( oem )
	{
		delete oem;
		oem = NULL;
	}

	oem = (Display_OEM *) new Display_VIA();
	if ( oem->IsSupported() )
	{
		oemDisplay = oem;
		return;
	} 
	
	if ( oem )
	{
		delete oem;
		oem = NULL;
	}
}



void CDisplayPropPage::SyncDisplaySettings()
{
	TCHAR setting[255];
	DWORD width,height,freq, curColor;

	GetCurrentDisplaySetting (setting);

	width  = 640;
	height = 480;
	freq   = 60;

	if ( CheckSign(TEXT("\\Windows\\macs.dat"), TEXT("\\Windows\\macs.sign")) )
	{
		_stscanf(setting, TEXT("%d x %d @%d"),&width,&height,&freq);
	}

	this->GetCurrentColorSetting (&curColor);
	this->oemDisplay->DisplayUpdate(width, height, curColor, freq);
}

void CDisplayPropPage::ResetDisplay2DefaultSetting()
{
	TCHAR *setting = NULL;
	DWORD  curColor;

	this->GetCurrentColorSetting (&curColor);
	this->oemDisplay->DisplayUpdate(800, 600, curColor, 60);
}
